package com.wx.hd;

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.UIManager;

public class demo {
    public static String filepath;
    public static List<String> listfiletype;
	private JFrame frmV;
	private JTextField textField_2;
	private JTextField textField;
	private JComboBox comboBox;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					demo window = new demo();
					window.frmV.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public demo() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmV = new JFrame();
		frmV.setFont(new Font("Dialog", Font.PLAIN, 18));
		frmV.setResizable(false);
		frmV.setTitle("\u64B8\u56FE\u5DE5\u5177\uFF08\u7FE0\u665A\uFF09 V1.0");
		frmV.setBounds(100, 100, 750, 648);
		frmV.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frmV.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent arg0) {
				System.exit(1);
			}
		});
		JPanel panel = new JPanel();
		panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 30));
		
		JLabel label = new JLabel("\u64B8\u56FE\u7F51\u7EDC\u5730\u5740");
		label.setFont(new Font("宋体", Font.BOLD, 18));
		label.setHorizontalAlignment(SwingConstants.LEFT);
		panel.add(label);
		
		textField_2 = new JTextField();
		textField_2.setColumns(50);
		panel.add(textField_2);
		
		JPanel panel_1 = new JPanel();
		panel_1.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 30));
		
		JLabel lblNewLabel_1 = new JLabel("\u64B8\u56FE\u4FDD\u5B58\u8DEF\u5F84");
		lblNewLabel_1.setFont(new Font("宋体", Font.BOLD, 18));
		lblNewLabel_1.setHorizontalAlignment(SwingConstants.LEFT);
		panel_1.add(lblNewLabel_1);
		
		textField = new JTextField();
		panel_1.add(textField);
		textField.setColumns(50);
		frmV.getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		frmV.getContentPane().add(panel);
		frmV.getContentPane().add(panel_1);
		
		JPanel panel_2 = new JPanel();
		frmV.getContentPane().add(panel_2);
		panel_2.setLayout(new FlowLayout(FlowLayout.CENTER, 35, 5));
		
		JPanel panel_7 = new JPanel();
		panel_2.add(panel_7);
		panel_7.setLayout(new GridLayout(0, 1, 0, 0));
		
		JLabel lblcup = new JLabel("撸图线程个数(cup核心数量)");
		lblcup.setHorizontalAlignment(SwingConstants.LEFT);
		panel_7.add(lblcup);
		lblcup.setFont(new Font("宋体", Font.BOLD, 18));
		
		JPanel panel_8 = new JPanel();
		panel_2.add(panel_8);
		GridBagLayout gbl_panel_8 = new GridBagLayout();
		gbl_panel_8.columnWidths = new int[] {240, 100};
		gbl_panel_8.rowHeights = new int[]{30, 0};
		gbl_panel_8.columnWeights = new double[]{0.0, Double.MIN_VALUE};
		gbl_panel_8.rowWeights = new double[]{0.0, Double.MIN_VALUE};
		panel_8.setLayout(gbl_panel_8);
		
		String[] count = { "  1", "  2", "  3", "  4", "  5", "  6", "  7", "  8" };
		comboBox = new JComboBox(count);
		comboBox.setMaximumRowCount(8);
		GridBagConstraints gbc_comboBox = new GridBagConstraints();
		gbc_comboBox.fill = GridBagConstraints.BOTH;
		gbc_comboBox.gridx = 0;
		gbc_comboBox.gridy = 0;
		panel_8.add(comboBox, gbc_comboBox);
		
		JPanel panel_3 = new JPanel();
		frmV.getContentPane().add(panel_3);
		panel_3.setLayout(new FlowLayout(FlowLayout.CENTER, 55, 30));
		
		JPanel panel_6 = new JPanel();
		panel_3.add(panel_6);
		panel_6.setLayout(new GridLayout(0, 1, 0, 0));
		
		JLabel label_1 = new JLabel("撸图文件后缀名（可以是其他文件不限于图片）");
		panel_6.add(label_1);
		label_1.setToolTipText("");
		label_1.setHorizontalAlignment(SwingConstants.CENTER);
		label_1.setFont(new Font("宋体", Font.BOLD, 18));
		
		JPanel panel_5 = new JPanel();
		panel_3.add(panel_5);
		panel_5.setLayout(new GridLayout(0, 2, 0, 0));
		JCheckBox checkBox_1 = new JCheckBox("bmp");
		panel_5.add(checkBox_1);
		
		JCheckBox checkBox_2 = new JCheckBox("gif");
		panel_5.add(checkBox_2);
		
		JCheckBox checkBox_3 = new JCheckBox("jpe?g");
		panel_5.add(checkBox_3);
		
		JCheckBox checkBox_4 = new JCheckBox("png");
		panel_5.add(checkBox_4);
		
		JCheckBox checkBox_5 = new JCheckBox("tiff?");
		panel_5.add(checkBox_5);
		
		JCheckBox checkBox_6 = new JCheckBox("mp3");
		panel_5.add(checkBox_6);
		
		JCheckBox checkBox_8 = new JCheckBox("mp4");
		panel_5.add(checkBox_8);
		
		JCheckBox checkBox_9 = new JCheckBox("avi");
		panel_5.add(checkBox_9);
		
		JCheckBox checkBox_10 = new JCheckBox("zip");
		panel_5.add(checkBox_10);
		
		JCheckBox checkBox = new JCheckBox("rar");
		panel_5.add(checkBox);
		
		JCheckBox checkBox_11 = new JCheckBox("pdf");
		panel_5.add(checkBox_11);
		
		JCheckBox checkBox_7 = new JCheckBox("xlsx?");
		panel_5.add(checkBox_7);
		
		JCheckBox checkBox_13 = new JCheckBox("docx?");
		panel_5.add(checkBox_13);
		
		JCheckBox checkBox_12 = new JCheckBox("pptx?");
		panel_5.add(checkBox_12);
		
		JCheckBox checkBox_14 = new JCheckBox("css");
		panel_5.add(checkBox_14);
		
		JCheckBox checkBox_15 = new JCheckBox("js");
		panel_5.add(checkBox_15);
		
		JCheckBox chckbxTxt = new JCheckBox("txt");
		panel_5.add(chckbxTxt);
		
		JPanel panel_4 = new JPanel();
		frmV.getContentPane().add(panel_4);
		panel_4.setLayout(new FlowLayout(FlowLayout.CENTER, 154, 30));
		
		JButton button = new JButton("开始撸图");
		button.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				try {
					String cb = comboBox.getSelectedItem().toString().trim();
					listfiletype = demo.getAllJCheckBoxValue(frmV, new ArrayList<String>());
					if(listfiletype != null && listfiletype.size()>0) {
						String filterStr = "";
						for (int i = 0; i < listfiletype.size(); i++) {
							if(i == (listfiletype.size()-1)) {
								filterStr += listfiletype.get(i);
							}else {
								filterStr += listfiletype.get(i)+"|";
							}
						}
						MyCrawler.filters = Pattern.compile(".*(\\.("+filterStr+"))$");
					}
					String t1 = textField_2.getText();
					if(t1 == null || "".equals(t1.trim())) {
						JOptionPane.showConfirmDialog(frmV, "请填写网络地址", "提示信息", JOptionPane.DEFAULT_OPTION);
						return;
					}
					String t2 =textField.getText();
					if(t2 == null || "".equals(t2.trim())) {
						JOptionPane.showConfirmDialog(frmV, "请填保存路径", "提示信息", JOptionPane.DEFAULT_OPTION);
						return;
					}
					filepath = t2;
					MyCrawler.uplaodImages(t1,cb);
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}
		});
		panel_4.add(button);
		button.setForeground(Color.DARK_GRAY);
		button.setFont(new Font("Dialog", Font.BOLD, 18));
		button.setBackground(Color.WHITE);
		JButton button_1 = new JButton("停止撸图");
		button_1.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				int stutas = JOptionPane.showConfirmDialog(frmV, "是否停止撸图", "提示信息", JOptionPane.YES_NO_OPTION);
				//销毁
				if(stutas == 0) {
					frmV.dispose();
				}
			}
		});

		panel_4.add(button_1);
		button_1.setForeground(UIManager.getColor("Button.foreground"));
		button_1.setFont(new Font("Dialog", Font.BOLD, 18));
		button_1.setBackground(Color.WHITE);
		
		
	}
	public static ArrayList<String> getAllJCheckBoxValue(Container ct, ArrayList<String> list){
        if(list==null){
            list=new ArrayList<String>();
        }
        int count=ct.getComponentCount();
        for(int i=0;i<count;i++){
            Component c=ct.getComponent(i);
            if(c instanceof JCheckBox){
                if(((JCheckBox)c).isSelected()) {
                	list.add(((JCheckBox)c).getText());
                }
            }else if(c instanceof Container){
                getAllJCheckBoxValue((Container)c,list);
            }
        }
        return list;
    }
}
